﻿using System;
using System.Collections.Generic;

namespace Game.Lib
{
    public class Parcel : Serializable
    {
        /// <summary>
        /// List of points that define the parcel
        /// </summary>
        /// <remarks>
        /// Only necessarly valid if <ref>Centralized</ref> is true
        /// </remarks>
        public List<Vector2> Points;
        
        /// <summary>
        /// List of land triangles making up the parcel
        /// </summary>
        public List<LandTriangle> LandTriangles;
        
        /// <summary>
        /// Owner of the parcel
        /// </summary>
        public UUID Owner;
        
        /// <summary>
        /// Miscilanious data about the parcel
        /// </summary>
        public ParcelData Data;
        
        /// <summary>
        /// Returns weather all points in all land triangles in this parcel have been centralized to this instances Points list
        /// </summary>
        public bool Centralized
        {
            get
            {
                return _Centralized;
            }
        }

        /// <summary>
        /// The area of the entire parcel
        /// </summary>
        public float Area
        {
            get
            {
                //TODO: Pick a better method of calculation
                float ret = 0f;
                foreach(LandTriangle lt in LandTriangles)
                {
                    ret += lt.Area;
                }
                return ret;
            }
        }
        
        public Vector2 Center
        {
            get
            {
                Vector2 sum = new Vector2();
                foreach(LandTriangle lt in LandTriangles)
                {
                    sum += lt.Center;
                }
                return sum / LandTriangles.Count;
            }
        }
        
        private bool _Centralized;

        public List<Parcel> Nehibors()
        {
            throw new NotImplementedException();
        }

        public Parcel()
        {
            LandTriangles = new List<LandTriangle>();
            LandTriangles.Add(new LandTriangle());
            Owner = UUID.NULL_KEY;
        }

        public Parcel(UUID owner, List<LandTriangle> landTris, bool doCentralize)
        {
            if(doCentralize)
            {
	            foreach(LandTriangle lt in landTris)
	            {
                    int index = Points.FindIndex(x => x.Equals(lt.Points[lt.P0]));
                    if(index == -1) Points.Add(lt.Points[lt.P0]);
                    
                    index = Points.FindIndex(x => x.Equals(lt.Points[lt.P1]));
                    if(index == -1) Points.Add(lt.Points[lt.P1]);
                    
                    index = Points.FindIndex(x => x.Equals(lt.Points[lt.P2]));
                    if(index == -1) Points.Add(lt.Points[lt.P2]);
                    
	                lt.UpdateIndices(Points);
	            }
            }
            else
            {
                Points = new List<Vector2>();
            }
            Owner = owner;
            LandTriangles = landTris;
            _Centralized = doCentralize;
        }

        public void FromBytes(byte[] data, int pos)
        {
            throw new NotImplementedException();
        }

        public byte[] ToBytes()
        {
            throw new NotImplementedException();
        }

        public void Serialize(System.IO.Stream s)
        {
            throw new NotImplementedException();
        }

        public void Deserialize(System.IO.Stream s)
        {
            throw new NotImplementedException();
        }
    }
}
